//
//  MainRequestAPI.m
//  BaseProject
//
//  Created by Macx on 2018/2/24.
//  Copyright © 2018年 Gavin. All rights reserved.
//

#import "MainRequestAPI.h"
#import "NetworkConfig.h"

@interface MainRequestAPI()

@property (nonatomic ,strong) NSString *theApiKey;

@property(nonatomic ,strong) NSDictionary *header;//2023.8.1增加

@property (nonatomic ,strong) NSMutableDictionary *argumentsDic;

@property (nonatomic ,assign) RequestMethod methodType;

@end

@implementation MainRequestAPI

- (instancetype)initWithAPIKey:(NSString *)apiKey
                        header:(NSDictionary*)header
                      argument:(NSDictionary *)arguments
                 requestMethod:(RequestMethod)method{
    
    self = [super init];
    
    if (self) {
        
        _theApiKey = @"";
        _argumentsDic = [NSMutableDictionary dictionary];
        _methodType = method;
        
        if ([apiKey length]>0) {
            _theApiKey = apiKey;
        }
        
        if (arguments) {
            [_argumentsDic addEntriesFromDictionary:arguments];
            NSLog(@"--->request para:%@",arguments);
        }
        if(header){
            NSLog(@"--->request header para:%@",header);
            self.header = header;
        }
    }

    return self;
}

- (NSString *)requestUrl {
    if (_argumentsDic.allKeys.count == 1&&
        [_argumentsDic.allKeys containsObject:@"array"]&&
        _methodType==RequestMethodPOST&&
        [PrivateMange share].isLogined) {
        /*_theApiKey = [[_theApiKey stringByAppendingString:@"?jwt="] stringByAppendingString:[EduOnlineManage shareEduOnline].userModel.token];*/
    }
    return _theApiKey;
}

- (RequestMethod)requestMethod {
    return _methodType;
}

- (id)requestArgument {
    
    /*NSString *_sid = [self getUserUUID];
    NSString *_t = [self getCurrentMinSecondTimestamp];
    NSString *_sign = [self GethashStrWithKey:[NSString stringWithFormat:@"%@&%@&%@",_sid,@"",_t]];
    NSDictionary *dic = @{@"loginMethod":@"2",
                          @"_sign":_sign,
                          @"_sid":_sid,
                          @"_t":_t};
    
    [_argumentsDic addEntriesFromDictionary:dic];*/
    if (_argumentsDic.allKeys.count == 1&&
        [_argumentsDic.allKeys containsObject:@"array"]&&
        _methodType==RequestMethodPOST) {
        return _argumentsDic[@"array"];
    }else{
        if ([PrivateMange share].isLogined) {
            //NSDictionary *dic = @{@"jwt":[EduOnlineManage shareEduOnline].userModel.token};
            //[_argumentsDic addEntriesFromDictionary:dic];
        }
        return _argumentsDic;
    }
    
}

- (NSTimeInterval)requestTimeoutInterval{
    return 60;
}

- (NSDictionary *)requestHeaderFieldValueDictionary {
    NSString *token = [NSString stringWithFormat:@"Bearer %@",[UserDefaults objectForKey:kToken]];
    
    NSMutableDictionary *mtb = [[NSMutableDictionary alloc]initWithDictionary:@{@"Accept-Language":[NetworkConfig sharedConfig].language,
                                   @"brandId":[NetworkConfig sharedConfig].brandID,
                                   @"Authorization":token,
                                   @"deviceType":@"iOS",
                                   @"deviceName":SafeStr([UIDevice currentDevice].name),
                                   @"systemVersion":[[UIDevice currentDevice] systemVersion],
                                   @"Version":[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"],
                                   @"platform":@"business"

                                                                              }];
    NSLog(@"--->request header:%@",mtb);
    //@"Content-Type":@"application/x-www-form-urlencoded"
    if(_header){
        [mtb addEntriesFromDictionary:_header];
    }
    return mtb;
}

- (NSString *)getCurrentMinSecondTimestamp
{
    UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
    
    NSString *timeLocal = [[NSString alloc] initWithFormat:@"%llu", recordTime];
    //    NSLog(@"获取毫秒级的时间戳 = %@", timeLocal);
    return timeLocal;
}

- (NSString *)GethashStrWithKey:(NSString *)key
{
    long long hash = 0;
    
    const char *ch = [key cStringUsingEncoding:NSASCIIStringEncoding];
    for (long long i = 0; i < strlen(ch); i++) {
        hash =( 1313 * hash + ch[i])%10000000000;
        
        //     NSLog(@"%@___________",[NSString stringWithFormat:@"%zd",hash]);
        
    }
    return [NSString stringWithFormat:@"%lld",hash];
    
}

-(NSString *)getUserUUID
{
    CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault);
    NSString*cfuuidString = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, cfuuid));
    //    NSString *uuid=[[NSUUID UUID] UUIDString];
    //    NSLog(@"获取生成的CFUUID----%@ UUID--%@",cfuuidString,uuid);
    return cfuuidString;
    
}


@end
